<?php

namespace app\api\controller;



use app\admin\controller\general\Profile;
use think\Db;
/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Hospital extends Base
{

    /**
     * Hospital模型对象
     * @var \app\admin\model\cyclopedia\Hospital
     */
    protected $noNeedLogin = ['*'];

    public function get_list2()
    {
        $city_code = $this->request->get('code','');
        $off_id = $this->request->get('office','');
        $h_model = new \app\common\model\Hospital();
        $where=[];
        $where['status']=1;
        if ($city_code){
           $where['city_id']  = $city_code;
        }
        if($off_id){
            $where['office'] = Db::raw("FIND_IN_SET('".$off_id."',office)");
        }
        $list = $h_model->where($where)->field('image,id,name,tag,city_id')->order('weight desc')->select();
        $list=collection($list)->toArray();
        if($list){
            foreach ($list as &$item){
                $tag = \app\admin\model\cyclopedia\Tag::where('id','in',$item['tag'])->select();
                $tag = collection($tag)->toArray();
                $item['tag']= $tag;
            }
        }

        $this->success('获取成功',$list,200);
    }
    public function get_list()
    {
        $city_code = $this->request->get('code','');
        $off_id = $this->request->get('office','');
        $h_model = new \app\common\model\Hospital();
        $where=[];
        $where['status']=1;
        if ($city_code){
            $where['city_id']  = $city_code;
        }
        $where2=[];
        if($off_id){
            $where2['office_id']  = $off_id;
        }
        // image,id,name,tag,city_id
        // $list = db('Hospital')->field('image,id,name,tag,city_id')->where('id','IN',function ($query)use($where2){
        //     $query->name('Officehos')->where($where2)->field('hospital_id')->order('ranking ASC');
        // })->where($where)->select();
        
        $ids = db('Officehos')->where($where2)->field('hospital_id')->order('ranking ASC')->column('hospital_id');
        $order="";
        $where3=[];
          if($ids){
              $ids = implode(',',$ids);
              $order = "field(id,$ids)";
              $where3['id'] = ['in',$ids];
          }
        // $exp = new Expression('field(id,' . implode(',', $ids) . ')');
        $list = db('Hospital')->field('image,id,name,tag,city_id')->where($where3)->where($where)->orderRaw($order)->select();
//
//        $list = $h_model
//            ->alias('h')
//            ->field('h.image,h.id,h.name,h.tag,h.city_id')
//            ->where($where)
//            ->join('officehos o','o.hospital_id=h.id','left')
//            ->order('o.ranking ASC')
//            ->count();
//        var_dump($list);die;
//        $list=collection($list)->toArray();
        if(!$off_id){
            if($list){
                foreach ($list as &$item){
                    $tag = \app\admin\model\cyclopedia\Tag::where('id','in',$item['tag'])->select();
                    $tag = collection($tag)->toArray();
                    $item['tag']= $tag;
    //                $item['image']=cdnurl($item['image'], true);
                }
            }
        }else{
            if($list){
            foreach ($list as &$item){
                $tag = \app\admin\model\cyclopedia\Tag::where('id','in',$item['tag'])->select();
                $tag = collection($tag)->toArray();
                $item['tag']= $tag;
//                $item['image']=cdnurl($item['image'], true);
                $has_rank = db('Officehos')->field('ranking')->where(['hospital_id'=>$item['id'],'office_id'=>$off_id])->find();
                if($has_rank){
                    $item['ranking']=$has_rank['ranking'];
                }else{
                    $item['ranking']=0;
                }
             }
            }
        }
        

        $this->success('获取成功',$list,200);
    }
    public function detail(){
        $id= $this->request->get('id','');
        if(!$id){
            $this->error('参数错误');
        }
        $where=[];
        $where['status'] = 1;
        $where['id'] = $id;
        $info = db('hospital')->where($where)->order('weight desc')->find();
        if(!$info){
            $this->error('医院未查询到');
        }
        $order   = array("\r\n", "\n", "\r");
        $info['address'] =str_replace($order,'<br/>',$info['address']);
        $info['phone'] =str_replace($order,'<br/>',$info['phone']);
        $info['tag']=db('tag')->where('id','in',$info['tag'])->select();
        $this->success('获取成功',$info,200);
    }
}
